'use strict'

let errorTypeDeclarations = {
    InternalException: {
        id: 1,
        message: 'Server internal exception'
    },
    UsernamePasswordMismatched: {
        id: 2,
        message: 'User name and password are mismatched'
    },
    UserNotLogin: {
        id: 3,
        message: 'User has not logined into system'
    },
    NoPrivileges: {
        id: 4,
        message: 'User has no correct privileges.'
    },
    RoleCanNotFound: {
        id: 5,
        message: 'Role can not be found.'
    },
    UserCanNotFound: {
        id: 6,
        message: 'User can not be found.'
    },
    PrivilegeCanNotFound: {
        id: 7,
        message: 'Privilege can not be found.'
    },
    CounterNameNotFound: {
        id: 8,
        message: 'The counter name is not found.'
    },
    FieldsError: {
        id: 9,
        message: 'The format of fields is incorrect.'
    },
    CheckTimeSetTypeError: {
        id: 10,
        message: 'The type of check time set is wrong'
    },
    AbsenceApplyCanNotModified: {
        id: 11,
        message: 'The absence apply can not be modified'
    },
    NoUser: {
        id: 12,
        message: 'The user not exists'
    },
    StudentNotFound: {
        id: 13,
        message: 'The student is not found'
    },
    CardNotFound: {
        id: 14,
        message: 'The card is not found'
    },
    CardNotBound: {
        id: 15,
        message: 'The card is not bound'
    },
    NotLeaderOfSchool: {
        id: 16,
        message: 'You are not the leader of the school'
    }, 
    DeviceNotFound: {
        id: 16,
        message: 'The device is not found'
    },
    LabelNotFound: {
        id: 17,
        message: 'The label is not found'
    },
    RuleNotFound: {
        id: 18,
        message: 'The rule is not found'
    }
};


function makeExceptionClass(exceptionDeclaration) {
    let id = exceptionDeclaration.id;
    let message = exceptionDeclaration.message;
    return function() {
        this.id = id;
        this.message = message;
    };
}

let exceptionClasses = Object.create(null);
let exceptionNames = Object.keys(errorTypeDeclarations);

exceptionNames.forEach(exceptionName => {
    let declaration = errorTypeDeclarations[exceptionName];
    exceptionClasses[exceptionName] = makeExceptionClass(declaration);
});

let exceptionObjects = Object.create(null);
exceptionNames.forEach(exceptionName => {
    let clazz = exceptionClasses[exceptionName];
    exceptionObjects[exceptionName] = new clazz();
});
exceptionObjects.classes = exceptionClasses;

module.exports = exceptionObjects;

